/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.example.omsdkdemo.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.device.idea.om.controller.model.StorageUsageStats;
import com.example.omsdkdemo.R;
import com.example.omsdkdemo.manager.OmSdkManager;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 功能描述
 *
 * @since 2023-02-01
 */
public final class DeviceDialogFragment extends BaseDialogFragment {
    private static final String TAG = "DeviceDialog";

    private static final String FORMAT = "total:%d,used:%d,free:%d,usage rate:%.2f%%";

    private static final int PERCENT = 100;

    private static final long KB = 1L;

    private static final long MB = 1024L;

    private static final long GB = 1024 * 1024L;

    private static final Map<Integer, Long> UNIT_MAP = new HashMap<>();

    private Button tvStorageUsageTitle;

    private TextView tvStorageUsage;

    private RadioGroup storageUnit;

    static {
        UNIT_MAP.put(R.id.device_usage_storage_kb, KB);
        UNIT_MAP.put(R.id.device_usage_storage_mb, MB);
        UNIT_MAP.put(R.id.device_usage_storage_gb, GB);
    }

    public DeviceDialogFragment(OmSdkManager omSdkManager) {
        super(R.layout.device_usage_dialog, omSdkManager.getContext());
        this.omSdkManager = omSdkManager;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        initView();
        initListener();
        builder.setTitle(R.string.device_info).setView(mView).setPositiveButton("OK", (dialog, which) -> {
        });
        return builder.create();
    }

    /**
     * initView
     */
    @Override
    protected void initView() {
        super.initView();
        tvStorageUsage = mView.findViewById(R.id.tv_storage_usage);
        tvStorageUsageTitle = mView.findViewById(R.id.tv_storage_usage_title);

        storageUnit = (RadioGroup) mView.findViewById(R.id.device_usage_storage_unit);
        storageUnit.setOnCheckedChangeListener((group, checkedId) -> {
            StorageUsageStats storageUsage = omSdkManager.getStorageUsageStats();
            tvStorageUsage.setText(formatInfo(storageUsage.getTotal(), storageUsage.getUsed(), storageUsage.getFree()));
        });
    }

    private void initListener() {
        tvStorageUsageTitle.setOnClickListener(v -> {
            StorageUsageStats storageUsage = omSdkManager.getStorageUsageStats();
            tvStorageUsage.setText(formatInfo(storageUsage.getTotal(), storageUsage.getUsed(), storageUsage.getFree()));
        });
    }

    private String formatInfo(long total, long used, long free) {
        if (storageUnit == null) {
            return "";
        }
        long unit = UNIT_MAP.getOrDefault(storageUnit.getCheckedRadioButtonId(), KB);
        return String.format(Locale.getDefault(), FORMAT, total / unit, used / unit, free / unit,
            (float) used * PERCENT / total);
    }
}
